package com.dennytech.tac.ui.activity;

import org.json.JSONObject;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.dennytech.tac.base.CFListActivity;
import com.dennytech.tac.model.TravelGuide;
import com.dennytech.tac.net.api.TravelGuideListApi;
import com.dennytech.tac.statistics.Stat;
import com.dennytech.tac.ui.adapter.BasicLoadAdapter;
import com.dennytech.tac.widget.TravelGuideListItem;
import com.dennytech.tac.widget.SearchBarEditText.OnKeywordChangeListner;
import com.dennytech.tac.R;

public class TravelGuideListActivity extends CFListActivity implements
		OnItemClickListener, OnKeywordChangeListner {

	Adapter adapter;
	Task task;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Uri uri = getIntent().getData();
		String keyword = uri.getQueryParameter("k");
		if (keyword != null) {
			showSearchEdt(R.string.hint_please_input_the_keyword, keyword,
					TravelGuideListActivity.this);
		}

		setRightTitleButton(R.drawable.ic_search, new OnClickListener() {

			@Override
			public void onClick(View v) {
				showSearchEdt(R.string.hint_please_input_the_keyword, null,
						TravelGuideListActivity.this);
				
				Stat.event("travelguidelist", "travelguidelist_titlebtn", null, 0);
			}
		});

		adapter = new Adapter();
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
	}

	Handler mHandler = new Handler() {
		String keword = "";
		
		public void handleMessage(android.os.Message msg) {
			if (msg.obj == null) {
				return;
			}
			
			String k = ((String) msg.obj).trim();
			if (k.equals(this.keword)) {
				return;
			}
			
			this.keword = k;
			adapter.keyword = k;
			adapter.reset();
			
			Stat.event("travelguidelist", "travelguidelist_search", k, 0);
		};
	};

	@Override
	public void onKeywordChanged(String keyword) {
		adapter.cancelLoad();

		String k = keyword.trim();
		mHandler.removeMessages(0);
		if ("".equals(k)) {
			mHandler.sendMessage(mHandler.obtainMessage(0, k));
		} else {
			mHandler.sendMessageDelayed(mHandler.obtainMessage(0, k), 800);
		}
	}

	@Override
	protected void onDestroy() {
		adapter.cancelLoad();
		super.onDestroy();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Object item = adapter.getItem(position);
		if (!(item instanceof TravelGuide)) {
			return;
		}

		Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("denny://tginfo"));
		i.putExtra("tg", (TravelGuide) adapter.getItem(position));
		startActivity(i);
		
		Stat.event("travelguidelist", "travelguidelist_search", String.valueOf(position), 0);
	}

	class Task extends BasicLoadAdapter.NetworkTask {
		String keyword;

		public Task(Adapter adapter) {
			super(adapter);
		}

		public void setKeyword(String k) {
			if ("".equals(k)) {
				keyword = null;
			} else {
				keyword = k;
			}
		}

		@Override
		public JSONObject doInBackground(Integer... params) {
			((TravelGuideListApi) api).setKeyword(keyword);
			return ((TravelGuideListApi) api).query();
		}

		@Override
		protected TravelGuideListApi creatApi() {
			return new TravelGuideListApi();
		}
	}

	class Adapter extends BasicLoadAdapter<TravelGuide> {

		String keyword;
		
		@Override
		public int getCount() {
			if (super.getCount() == 0) {
				return 1;
			}
			return super.getCount();
		}
		
		@Override
		public Object getItem(int position) {
			if (position < mData.size()) {
				return mData.get(position);
			}

			if (mData.size() == 0 && !TextUtils.isEmpty(mEmptyMsg)) {
				return EMPTY;
			}
			
			return super.getItem(position);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = super.getView(position, convertView, parent);
			if (view instanceof TravelGuideListItem) {
				TravelGuide tg = (TravelGuide) getItem(position);
				((TravelGuideListItem) view).setTravelGuide(tg);
			}
			return view;
		}

		@Override
		protected View itemViewForData(View convertView, ViewGroup parent) {
			if (convertView != null
					&& convertView instanceof TravelGuideListItem) {
				return convertView;
			}

			return LayoutInflater.from(parent.getContext()).inflate(
					R.layout.travelguide_list_item, parent, false);
		}

		@Override
		public com.dennytech.tac.ui.adapter.BasicLoadAdapter.NetworkTask createTask() {
			Task task = new Task(this);
			if (keyword != null) {
				task.keyword = this.keyword;
			}
			return task;
		}

		@Override
		protected TravelGuide decodeObject(JSONObject object) {
			return TravelGuide.decode(object);
		}
	}

}
